#ifndef __TCPSERVER_H__
#define __TCPSERVER_H__
#include "MainReactor.h"
#include "ThreadPool.h"
#include "Timer.h"
#include "LoadBalanceStrategy.h"
#include <memory>
#include <functional>
#include <chrono>

using std::shared_ptr;
using std::function;
using std::string;

class TcpConnection;
class TcpServer
{
public:
    using TcpConnectionPtr = shared_ptr<TcpConnection>;
    using TcpConnectionCallback = function<void(const TcpConnectionPtr &)>;
    // TcpServer(const string &ip, unsigned short port, int subReactorNum = 1, int threadNum = 4, int queueSize = 1024);
    TcpServer(const string &ip, unsigned short port, int subReactorNum = 1, ThreadPool *threadPool = nullptr);
    ~TcpServer();
    void start();
    void stop();
    void setAllCallback(TcpConnectionCallback &&cb1, 
                        TcpConnectionCallback &&cb2,
                        TcpConnectionCallback &&cb3);
    
    // 定时器接口
    int64_t runAfter(std::chrono::milliseconds delay, TimerCallback &&callback);
    int64_t runEvery(std::chrono::milliseconds interval, TimerCallback &&callback);
    void cancelTimer(int64_t timerId);
    
    // 负载均衡接口
    void setLoadBalanceStrategy(LoadBalanceStrategy strategy);
    LoadBalanceStrategy getLoadBalanceStrategy() const;
    std::pair<size_t, size_t> getConnectionRange();
    
    // 连接数管理
    void setMaxConnections(size_t maxConns);
    size_t getMaxConnections() const;

private:
    MainReactor _mainReactor;
    ThreadPool *_threadPool;
};


#endif

